package com.mrdeer.iot.bean.command;

import com.mrdeer.iot.bean.enums.BinValue;
import com.mrdeer.iot.utils.HexHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 14个字节 的设备状态
 * <p>
 * 测试 数据
 * 68 01 01 0A 00 00 00 00 00 01 01 5D 02 01 A1 B1
 * 68 01 01 15 00 00 00 00 00 01 01 [01 02 01 01 01 01 01 01 01 01 01 01 01 01] A1 B1
 * 68 01 01 15 00 00 00 00 00 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 A1 B1
 * 68 01 01 15 00 00 00 00 00 01 04 00 00 01 02 01 01 01 02 01 01 01 01 01 01 01 01 A1 B1
 *
 * @Author: xie jianchu
 */
@Slf4j
@Data
public class DeviceStatus {

    private String controlFrame;            //控制信息

    private MachineStatus all_machine_status;     //整机状态

    private WashStoreStatus wash_store_status;    //洗涤仓状态

    private long wash_time;      //洗涤剩余时间

    /**
     * 00 待机/未启动
     * 01 浸泡
     * 02 洗涤
     * 03 漂洗
     * 04 脱水
     */
    private String wash_stage;     //洗涤仓阶段
    /**
     * 00 空流程
     * 01 智能全自动
     */
    private String wash_program;   //洗涤程序

    private DryStoreStatus drying_1_status;   //1号烘干仓状态

    private long drying_1_time;     //1号烘干仓剩余时间

    /**
     * 00 待机/未启动
     * 01 浸泡
     * 02 洗涤
     * 03 漂洗
     * 04 脱水
     */
    private String drying_1_stage;    //1号烘干仓阶段

    private String drying_1_program;  //1号烘干仓程序

    private DryStoreStatus drying_2_status;   //2号烘干仓状态

    private long drying_2_time;     //2号烘干仓剩余时间

    private String drying_2_stage;    //2号烘干仓阶段

    private String drying_2_program;  //2号烘干仓程序

    /**
     * 00 报警解除
     * 01 排水超时
     * 02 开盖报警
     * 03 进水超时
     * 04 水位传感器异常
     * 05 水位溢出报警
     * 06 洗鞋液不足报警
     */
    private ErrorCode errorCode;         //故障码

    public DeviceStatus(String data) {
        if (data == null && data.length() >= 32) {
            log.error("状态数据小于16位,数据错误");
            return;
        }
        //00000102010101020101010101010101
        //0000
        controlFrame = data.substring(0, 4);
        //01
        all_machine_status = new MachineStatus(HexHelper.hexToBin(data.substring(4, 6)));
        //02
        wash_store_status = new WashStoreStatus(HexHelper.hexToBin(data.substring(6, 8)));
        //01
        wash_time = HexHelper.hexStr2Long(data.substring(8, 10));
        //01
        wash_stage = data.substring(10, 12);
        //01
        wash_program = data.substring(12, 14);
        //02
        drying_1_status = new DryStoreStatus(HexHelper.hexToBin(data.substring(14, 16)));
        //01
        drying_1_time = HexHelper.hexStr2Long(data.substring(16, 18));
        //01
        drying_1_stage = data.substring(18, 20);
        //01
        drying_1_program = data.substring(20, 22);
        //01
        drying_2_status = new DryStoreStatus(HexHelper.hexToBin(data.substring(22, 24)));
        //01
        drying_2_time = HexHelper.hexStr2Long(data.substring(24, 26));
        //01
        drying_2_stage = data.substring(26, 28);
        //01
        drying_2_program = data.substring(28, 30);
        //01
        errorCode = new ErrorCode(data.substring(30, 32));
    }

    @Data
    public class MachineStatus {

        public MachineStatus(String machineStatus) {
            String first_end = machineStatus.substring(machineStatus.length() - 1);
            String second_end = machineStatus.substring(machineStatus.length() - 2, machineStatus.length() - 1);
            String third_end = machineStatus.substring(machineStatus.length() - 3, machineStatus.length() - 2);
            if (first_end.equals(BinValue.ONE.getValue())) {
                isWash = true;
            } else if (first_end.equals(BinValue.ZERO.getValue())) {
                isWash = false;
            }
            if (second_end.equals(BinValue.ZERO.getValue())) {
                isMute = true;
            } else if (second_end.equals(BinValue.ONE.getValue())) {
                isMute = false;
            }
            if (third_end.equals(BinValue.ZERO.getValue())) {
                isHave = false;
            } else if (second_end.equals(BinValue.ONE.getValue())) {
                isHave = true;
            }
        }

        private boolean isWash;     //洗涤是否启动
        private boolean isMute;     //是否静音
        private boolean isHave;     //是否有洗涤剂
    }

    @Data
    public class WashStoreStatus {

        public WashStoreStatus(String washStoreStatus) {
            String first_end = washStoreStatus.substring(washStoreStatus.length() - 1);
            String second_end = washStoreStatus.substring(washStoreStatus.length() - 2, washStoreStatus.length() - 1);
            String third_end = washStoreStatus.substring(washStoreStatus.length() - 3, washStoreStatus.length() - 2);
            if (first_end.equals(BinValue.ZERO.getValue())) {
                isWashStoreStart = false;
            } else if (first_end.equals(BinValue.ONE.getValue())) {
                isWashStoreStart = true;
            }
            if (second_end.equals(BinValue.ZERO.getValue())) {
                isAuthorize = true;
            } else if (second_end.equals(BinValue.ONE.getValue())) {
                isAuthorize = false;
            }
            if (third_end.equals(BinValue.ZERO.getValue())) {
                isLockDoor = false;     //门盖解锁
            } else if (second_end.equals(BinValue.ONE.getValue())) {
                isLockDoor = true;      //门盖上锁
            }
        }

        private boolean isWashStoreStart;       //洗涤仓是否启动
        private boolean isAuthorize;            //是否授权
        private boolean isLockDoor;             //门盖是否解锁
    }

    @Data
    public class DryStoreStatus {

        public DryStoreStatus(String washStoreStatus) {
            String first_end = washStoreStatus.substring(washStoreStatus.length() - 1);
            String second_end = washStoreStatus.substring(washStoreStatus.length() - 2, washStoreStatus.length() - 1);
            if (first_end.equals(BinValue.ZERO.getValue())) {
                isDryStart = false;
            } else if (first_end.equals(BinValue.ONE.getValue())) {
                isDryStart = true;
            }
            if (second_end.equals(BinValue.ZERO.getValue())) {
                isAuthorize = true;
            } else if (second_end.equals(BinValue.ONE.getValue())) {
                isAuthorize = false;
            }
        }

        private boolean isDryStart;             //洗涤仓是否启动
        private boolean isAuthorize;            //是否授权
    }

    @Data
    public class ErrorCode {
        public ErrorCode(String code) {
            this.code = code;
        }

        private String code;
    }

}
